import 'package:cbec_app/components/http/network.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import '../../../components/base/base_ctr.dart';
import '../../../utils/Tools.dart';
import '../model/notice_model_entity.dart';

class NoticeCtr extends BaseController {
  int pageNum = 1;
  int pageSize = 10;
  bool hasNextPage = true;
  List<NoticeModelEntity> dataList = [];
  RefreshController baseRefreshCtr = RefreshController(initialRefresh: false);

  @override
  void onInit() {
    super.onInit();
    getNoticeList();
  }

  @override
  void onPageRefresh() async {
    super.onPageRefresh();
    pageNum = 1;
    getNoticeList();
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
  }

  @override
  void onPageLoading() async {
    super.onPageLoading();
    if (hasNextPage) {
      pageNum += 1;
      getNoticeList();
      baseRefreshCtr.loadComplete();
    }
  }

  void getNoticeList() {
    Map<String, dynamic> params = {
      "pageNum": pageNum,
      "pageSize": pageSize,
    };
    Tools.showLoading();
    Network.getNoticeList(params).then((value) {
      List<NoticeModelEntity> list = (value['list'] as List).map((item) => NoticeModelEntity.fromJson(item)).toList();
      hasNextPage = value['hasNextPage'];
      if (pageNum == 1) {
        dataList = list;
      } else {
        dataList.addAll(list);
      }
      update();
      Tools.hideLoading();
    }).catchError((error) {
      print('$error');
      Tools.hideLoading();
    });
  }
}
